//
//  SearchBarView.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/17.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "SearchBarView.h"

@interface SearchBarView ()<UITextFieldDelegate>

@property (nonatomic, copy) SearchBlack searchBlock;
@property (nonatomic, strong) UIImageView * leftImageView; // 左侧图片
@property (nonatomic, strong) UITextField * searchTextField; // 搜索文本输入框

@end

@implementation SearchBarView

#pragma mark 🌷🌷🌷life cycle🌷🌷🌷
- (instancetype)initWithSearchStr:(SearchBlack)searchStr
{
    self = [super init];
    if (self) {
        self.backgroundColor = RGB_COLOR(221, 224, 224, 1);
        
        _searchBlock = searchStr;
        // 添加左侧视图
        [self addSubview:self.leftImageView];
        // 添加右侧搜索文本输入框
        [self addSubview:self.searchTextField];
        
        // 自动布局
        [_leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.sizeOffset(CGSizeMake(SCREEN_HEIGHT * 0.08 * 2.4, SCREEN_HEIGHT * 0.07));
            make.left.offset(SCREEN_WIDTH * 0.01);
            make.bottom.offset(0);
        }];
        [_searchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.sizeOffset(CGSizeMake(SCREEN_WIDTH * 0.205, SCREEN_HEIGHT * 0.038));
            make.right.offset(SCREEN_WIDTH * -0.01);
            make.top.offset(SCREEN_HEIGHT * 0.04);
        }];
    }
    return self;
}

#pragma mark 🌷🌷🌷UITextFieldDelegate🌷🌷🌷
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    _searchBlock(textField.text);
    return YES;
}

#pragma mark 🌷🌷🌷getter and setter🌷🌷🌷
- (UIImageView *)leftImageView {
    if (!_leftImageView) {
        _leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
    }
    return _leftImageView;
}
- (UITextField *)searchTextField {
    if (!_searchTextField) {
        _searchTextField = [[UITextField alloc] init];
        // 设置背景颜色
        _searchTextField.backgroundColor = [UIColor whiteColor];
        // 设置代理
        _searchTextField.delegate = self;
        // 设置占位符
        _searchTextField.placeholder = @"请输入课程名搜索";
        // 设置return按钮样式
        _searchTextField.returnKeyType = UIReturnKeySearch;
        // 右侧占位视图
        UIImageView * rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search_menu.png"]];
        rightView.frame = CGRectMake(0, 0, SCREEN_WIDTH * 0.03, SCREEN_WIDTH * 0.03);
        _searchTextField.rightView = rightView;
        _searchTextField.rightViewMode = UITextFieldViewModeAlways;
        // 设置字体大小
        _searchTextField.font = [UIFont systemFontOfSize:22];
    }
    return  _searchTextField;
}

@end
